//
//  YKAlertToast.m
//  YKAlert
//
//  Created by edward on 2020/8/21.
//  Copyright © 2020 edward. All rights reserved.
//

#import "YKAlertToast.h"
#import <pthread.h>
#import <objc/message.h>

@implementation YKAlertToast


+ (void)showMessage:(NSString *)message inView:(UIView *)view {
    
    // 这里加个先删除Loading吧
    dispatch_async(dispatch_get_main_queue(), ^{
        [self dissLoading];
        if (message.length == 0) {
            return;
        }
        
        // 删除旧的先
        UIView *beforeView = [view viewWithTag:[YKAlertToast kYK_AlertMessage_Tag]];
        if (beforeView) {
            [beforeView removeFromSuperview];
            beforeView = nil;
        }
        
        BOOL isPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
        CGFloat showW = isPad?200:150;
        CGFloat showH = isPad?45:25;
        CGFloat spacX = isPad?10:10;
        
        UIFont *showFont = [UIFont systemFontOfSize:isPad?24:16];
        CGSize theStringSize = [message boundingRectWithSize:CGSizeMake(showW-spacX*2, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: showFont} context:nil].size;
        if (theStringSize.height<showH) {
            theStringSize.height = showH;
        }
        
        // 背景
        UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, showW, theStringSize.height+spacX*2)];
        showView.tag = [YKAlertToast kYK_AlertMessage_Tag];
        showView.alpha = 0;
        showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        showView.layer.cornerRadius = isPad?10:7;
        showView.layer.masksToBounds = YES;
        showView.center = CGPointMake(CGRectGetWidth(view.frame)/2, CGRectGetHeight(view.frame)/2);
        [view addSubview:showView];
        
        // 内容
        UILabel *showLabel = [[UILabel alloc] initWithFrame:CGRectMake(spacX, spacX, showW-spacX*2, theStringSize.height)];
        showLabel.textAlignment = NSTextAlignmentCenter;
        showLabel.numberOfLines = 0;
        showLabel.text = message;
        showLabel.textColor = [UIColor whiteColor];
        showLabel.backgroundColor = [UIColor clearColor];
        showLabel.font = showFont;
        [showView addSubview:showLabel];
        
        // 动画
        __block UIView *tempView = showView;
        [CATransaction begin];
        [CATransaction setCompletionBlock:^{
            [tempView removeFromSuperview];
            tempView = nil;
        }];
        CAKeyframeAnimation *keyAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
        keyAnim.values = @[@0,@1,@1,@0];
        keyAnim.keyTimes = @[@0,@0.1,@0.9,@1];
        keyAnim.duration = 2;
        keyAnim.repeatCount = 1;
        [showView.layer addAnimation:keyAnim forKey:nil];
        [CATransaction commit];
    });
    
}

+ (void)showMessage:(NSString *)message {
    
    [[self class] showMessage:message inView:[UIApplication sharedApplication].keyWindow];
}

+ (void)showLoading:(NSString *)message {
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        BOOL isPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
        CGFloat showW = isPad?200:150;
        CGFloat showH = isPad?45:25;
        CGFloat spacX = isPad?10:10;
        CGFloat activityW = 20;
        
        UIFont *showFont = [UIFont systemFontOfSize:isPad?24:16];
        CGSize theStringSize = [message boundingRectWithSize:CGSizeMake(showW-spacX*2-activityW, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: showFont} context:nil].size;
        
        if (theStringSize.height < showH) {
            theStringSize.height = showH;
        }
        
        UIView *keyWindow = [UIApplication sharedApplication].keyWindow;
        
        UIView *bgView = [keyWindow viewWithTag:[YKAlertToast kYK_AlertLoading_Tag]];
        UIView *showView = [bgView viewWithTag:[YKAlertToast kYK_AlertLoading_View_Tag]];
        UILabel *showLabel = [bgView viewWithTag:[YKAlertToast kKY_AlertLoading_Label_Tag]];
        UIActivityIndicatorView *activityView;
        BOOL needCreateView = bgView == nil;
        // 背景
        if (needCreateView) {
            bgView = [[UIView alloc] init];
            bgView.alpha = 0;
            bgView.tag = [YKAlertToast kYK_AlertLoading_Tag];
            bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
            [[UIApplication sharedApplication].keyWindow addSubview:bgView];
        }
        
        bgView.frame = CGRectMake(0, 0, CGRectGetWidth(keyWindow.frame), CGRectGetHeight(keyWindow.frame));
        
        if (message.length > 0) {
            
        } else {
            showW = theStringSize.height + spacX * 2;
        }
        
        if (needCreateView) {
            showView = [[UIView alloc] init];
            showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
            showView.layer.cornerRadius = isPad?10:7;
            showView.layer.masksToBounds = YES;
            showView.tag = [YKAlertToast kYK_AlertLoading_View_Tag];
            [bgView addSubview:showView];
        }
        
        showView.frame = CGRectMake(0, 0, showW, theStringSize.height + spacX * 2);
        showView.center = CGPointMake(CGRectGetWidth(keyWindow.frame)/2, CGRectGetHeight(keyWindow.frame)/2);
        
        // 内容
        if (message.length > 0) {
            if (needCreateView) {
                showLabel = [[UILabel alloc] init];
                showLabel.textAlignment = NSTextAlignmentCenter;
                showLabel.numberOfLines = 0;
                showLabel.textColor = [UIColor whiteColor];
                showLabel.backgroundColor = [UIColor clearColor];
                showLabel.font = showFont;
                showLabel.tag = [YKAlertToast kKY_AlertLoading_Label_Tag];
                [showView addSubview:showLabel];
            }
            showLabel.frame = CGRectMake(spacX+activityW, spacX, showW-spacX*2-activityW, theStringSize.height);
            showLabel.text = message;
        }
        
        // 状态
        if (needCreateView) {
            activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
            [showView addSubview:activityView];
        }
        if (message.length > 0) {
            activityView.center = CGPointMake(spacX+activityW/2, showView.frame.size.height/2);
        } else {
            activityView.center = CGPointMake(showView.frame.size.width/2.0f, showView.frame.size.height/2.0f);
        }
        
        [activityView startAnimating];
        
        // 动画
        if (needCreateView) {
            [UIView animateWithDuration:0.2 animations:^{
                bgView.alpha = 1;
            }];
        }
    });
    
    
}


+ (void)dissLoading {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIView *tempView = [[UIApplication sharedApplication].keyWindow viewWithTag:[YKAlertToast kYK_AlertLoading_Tag]];
        [tempView removeFromSuperview];
    });
}


+ (NSInteger)kYK_AlertLoading_Tag
{
    return 888;
}

+ (NSInteger)kYK_AlertMessage_Tag
{
    return 889;
}

+ (NSInteger)kYK_AlertLoading_View_Tag
{
    return 891;
}

+ (NSInteger)kKY_AlertLoading_Label_Tag
{
    return 890;
}

@end
